﻿using System;
using System.Web.UI;
using StockholmsStad.Web.UI.Class.Controller;
using StockholmsStad.Web.UI.Class.Facade;
using StockholmsStad.Web.UI.Class.Interfaces;
using StockholmsStad.Web.UI.Class.Model;
using StockholmsStad.Web.UI.Class.Utilities;
using StockholmsStad.Web.UI.UIServiceReference;

namespace StockholmsStad.Web.UI
{
    /// <summary>
    /// Websida för bokningen
    /// </summary>
    public partial class Bokning : Page
    {
        private BookingController _controller;

        /// <summary>
        /// Körs när sidan laddas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpSessionAdapter session = new HttpSessionAdapter(Session);
            WebServiceFacade facade = new WebServiceFacade();
            IBookingModel bookingModel = new BookingModel(session, facade);
            ILoginModel loginModel = new LoginModel(session, facade, new HttpContextAdapter(Context));
            _controller = new BookingController(bookingModel, loginModel, BokaTid3);
            BokaTid1.Controller = _controller;
            BokaTid3.Controller = _controller;
            btnChooseDateNext.Enabled = bookingModel.IsUpdateable();
        }

        /// <summary>
        /// Kanpptryck för 'nästa' vid välja datum
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChooseDateNext_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Booking booking = BokaTid1.Booking;

                //Validering av att minst ett datum är valt
                if (booking.DateTime != null || booking.PreferredDate1.HasValue || booking.PreferredDate2.HasValue)
                {
                    _controller.ChooseBookingTimes(booking);
                    if (_controller.IsLoggedIn)
                    {
                        mvwBooking.SetActiveView(viwConfirm);
                    }
                    else
                    {
                        mvwBooking.SetActiveView(viwProceedWithRegistration);
                    }
                }
            }
        }

        /// <summary>
        /// Kanpptryck för 'tillbaka' vid bekräfta bokning
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnConfirmPrev_Click(object sender, EventArgs e)
        {
            mvwBooking.SetActiveView(viwChooseDate);
        }

        /// <summary>
        /// Knapptryck för bekräfta bokning
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnConfirmNext_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                _controller.RegisterBooking();
                Response.Redirect("~/VarBokning/VarBokning.aspx");
            }
        }
    }
}